<?php

namespace App\Http\Controllers\WeChat;

use App\Http\Controllers\Controller;
use App\Models\WeChatNews;
use App\Models\WeChatRule;
use App\Services\WeChatService;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    //
    public function index()
    {
        $news = WeChatNews::site()->module()->get();
        return view('wechat.news.index', compact('news'));
    }

    public function show(WeChatNews $news)
    {
        return $news->contents;
    }

    public function store(Request $request, WeChatService $weChatService, WeChatNews $weChatNews)
    {
        $rule = $weChatService->save('news');
        $weChatNews->site_id = site()['id'];
        $weChatNews->module_id = module()['id'];
        $weChatNews->rule_id = $rule->id;
        $weChatNews->contents = json_decode($request->contents, true);
        $weChatNews->save();
        return redirect()->route('wechat.news.index')->with('保存成功');
    }

    public function create()
    {
        return view('wechat.news.create');
    }

    public function edit(WeChatNews $news)
    {

        return view('wechat.news.edit', compact('news'));
    }

    public function update(Request $request, WeChatService $weChatService, WeChatNews $news)
    {
        $weChatService->save('news');

        $news->contents = json_decode($request->contents, true);
        $news->save();
        return redirect()->route('wechat.news.index')->with('success','微信图文消息修改成功');
    }

}
